package com.YxyBackMange.Controller.SystemController;

import com.YxyBackMange.Dto.PageInfo;
import com.YxyBackMange.Dto.QueryInfo;
import com.YxyBackMange.Mapper.System.QueryMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class QueryController {
    private static final Logger logger = LoggerFactory.getLogger(QueryController.class);
    private static WebApplicationContext applicationContext=null;
    private QueryMapper queryMapper;
    @ResponseBody
    @RequestMapping("/Admin/{entityName}/query")
    public PageInfo query(QueryInfo queryInfo, @PathVariable("entityName") String entityName, HttpSession session, HttpServletRequest request) {
        System.out.println("--------------查询参数开始---------------");
        logger.info(queryInfo.toString());
        logger.info("--------------查询参数结束---------------");
        try {
            if(applicationContext==null)
            {
                applicationContext= WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
            }
            entityName=toLowerCaseFirstOne(entityName)+"Mapper";
            queryMapper= (QueryMapper) applicationContext.getBean(entityName);
            //设置分页和排序
            PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
            PageHelper.orderBy(queryInfo.getOrder());
            Page page= (Page) queryMapper.selectQueryInfo(queryInfo);
            PageInfo pageInfo = new PageInfo();
            List list=page.getResult();
            list.remove(null);
            pageInfo.setRows(list);
            pageInfo.setTotalRow(page.getTotal());
            pageInfo.setCurrentPage(queryInfo.getCurrentPage());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询失败:" + queryInfo.toString());
            return null;
        }

    }
    //首字母转小写
    private static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }


    //首字母转大写
    private static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }
}
